package com.ruijie.acl.web.validator;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;

import com.ruijie.acl.service.AclPlanParametersService;
import com.ruijie.foundation.international.MessageSupport;
 
/**
 * <p>
 * Title: 时间段名称验证
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 	时间段名称名称合法性验证
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-07-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("aclPlanNameValidator")
@Validator
public class AclPlanNameValidator extends MessageSupport implements javax.faces.validator.Validator {
	
	@In private Map<String, String> messages;

	@In("#{aclPlanParametersService}")
	private AclPlanParametersService aclPlanParametersService;

	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		String text = value.toString().trim();
		
		if(component.getAttributes().get("required").toString().equals("true")){
			if(!StringUtils.isNotEmpty(text.replaceAll("　",   " ").trim())){
				throw new ValidatorException(new FacesMessage(messages.get("javax.faces.component.UIInput.REQUIRED")));
			}
		}
		
		Long planId = (Long) component.getAttributes().get("planId");
		if(aclPlanParametersService.existBatchPlanWithName(text,planId)){
				throw new ValidatorException(new FacesMessage(getI18NMessage("batch_plan_name_exist")));
	    }
	}
}
